/**
 * 
 */
package com.yidin.lsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yidin.entry.MyDataEntry;
import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.MyPhotoDetailAdapter;
import com.yidin.entry.PhotoListEntry;
import com.yidin.entry.UserLoadGiftListEntry;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;
import com.yidin.util.NetImageBase;
import com.yidin.util.imageCallback;
import com.yidin.widget.Carema;
import com.yidin.widget.SlowGallery;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 用户信息显示
 * 
 * @author cola
 * 
 */
public class AboutMy extends Container {
	private Map<Button, View> group;
	private ViewGroup ViewContainer;
	private Button aboutBtn;
	private Button dynamicBtn;
	private Button photoBtn;
	private TextView uploadPhoto;
	private Button rightBtn;
	private Button leftBtn;
	private RelativeLayout myHomeBk;

	/**
	 * 粉丝
	 */
	private TextView LinkmeCount;
	/**
	 * 我的关注
	 */
	private TextView MylinkCount;
	/**
	 * 一句话介绍
	 */
	private TextView Introduction;
	private TextView Blood;
	private TextView Age;
	/**
	 * 收到的礼物数
	 */
	private TextView GiftCount;
	private TextView title;
	private TextView UserUpdateIntroduction;
	private TextView editTv;
	private ListView dynamicListView;
	private MyDynamicAdapter myDynamicAdapter;
	private PhotoAdapter photoAdapter;
	private GifAdapter gifAdapter;
	private List<MyDynamicEntry> l = null;// = new ArrayList<MyDynamicEntry>();
	private List<UserLoadGiftListEntry> m = null;
	private MyDataEntry me;
	private List<PhotoListEntry> p = null;

	public AboutMy(Activity activity, View contentView, Views father) {
		super(activity, contentView, father);
		// TODO Auto-generated constructor stub
		initialize();
	}

	@Override
	public void checkCache() {
		// TODO Auto-generated method stub

	}

	@Override
	public void findView(View contentView) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.mydata, null);

		ViewContainer.addView(view);
		this.aboutBtn = (Button) contentView.findViewById(R.id.ButtonAbout);
		group.put(this.aboutBtn, view);
		this.dynamicBtn = (Button) contentView.findViewById(R.id.ButtonDynamic);
		this.photoBtn = (Button) contentView.findViewById(R.id.ButtonPhoto);
		this.uploadPhoto = (TextView) contentView
				.findViewById(R.id.mydata_uploadPhoto);
		this.GiftCount = (TextView) contentView.findViewById(R.id.GiftCount);
		this.Blood = (TextView) contentView.findViewById(R.id.Blood);
		this.Age = (TextView) contentView.findViewById(R.id.Age);
		this.MylinkCount = (TextView) contentView
				.findViewById(R.id.MylinkCount);
		this.Introduction = (TextView) contentView
				.findViewById(R.id.Introduction);
		this.LinkmeCount = (TextView) contentView
				.findViewById(R.id.LinkmeCount);
		this.title = (TextView) contentView.findViewById(R.id.title);
		this.UserUpdateIntroduction = (TextView) contentView
				.findViewById(R.id.updateIntroduction);
		this.editTv = (TextView) contentView.findViewById(R.id.editData);
		this.leftBtn = (Button) contentView.findViewById(R.id.leftBtn);
		this.rightBtn = (Button) contentView.findViewById(R.id.rightBtn);
		this.myHomeBk = (RelativeLayout) contentView.findViewById(R.id.bkcontiner);
		dynamicListView = (ListView) view
				.findViewById(R.id.myFriendListView);
	}

	@Override
	public void showFunctionsListView(View contentView) {
		// TODO Auto-generated method stub
		this.group = new HashMap<Button, View>();
		ViewContainer = (ViewGroup) contentView
				.findViewById(R.id.ViewContainer);
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		this.aboutBtn.setOnClickListener(aboutOnClickListener);
		this.dynamicBtn.setOnClickListener(dynamicOnClickListener);
		this.photoBtn.setOnClickListener(myPhotoOnClickListener);
		this.uploadPhoto.setOnClickListener(uploadPhotoOnClickListener);
		this.UserUpdateIntroduction.setOnClickListener(uIntroduction);
		this.editTv.setOnClickListener(editOnClickListener);

		this.rightBtn.setBackgroundDrawable(null);
		this.rightBtn.setText("编辑");
		this.rightBtn.setTextColor(Color.WHITE);
		this.rightBtn.setOnClickListener(editOnClickListener);
		
		Map obj = new HashMap<String, String>();
		obj.put("AccountId", lscManager.getInstance().myAccount.name);

		//((LscActivity) activity).showProgress(activity);
		Task task = new Task(Task.TASK_USER_HOME, obj);
		MainService.AddNewTask(task);
	}

	private OnClickListener editOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, EditMyData.class);
			activity.startActivity(it);
		}

	};

	private OnClickListener uIntroduction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, UserUpdateIntroduction.class);
			it.putExtra("AccountId", "hlf");
			activity.startActivity(it);
		}

	};

	private OnClickListener uploadPhotoOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, UserUpdateIntroduction.class);
			activity.startActivity(it);
		}

	};

	private OnClickListener dynamicOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (group.get(v) == null) {
				group.get(aboutBtn).setVisibility(View.GONE);
				if (group.get(photoBtn) != null)
					group.get(photoBtn).setVisibility(View.GONE);
				View view = inflater.inflate(R.layout.listview, null);
				dynamicListView = (ListView) view
						.findViewById(R.id.myFriendListView);
				group.put((Button) v, view);
				group.get(aboutBtn).setVisibility(View.GONE);
				ViewContainer.addView(view);
			} else {
				((ViewGroup) group.get(aboutBtn)).setVisibility(View.GONE);
				if (group.get(photoBtn) != null)
					group.get(photoBtn).setVisibility(View.GONE);
				group.get(dynamicBtn).setVisibility(View.VISIBLE);
			}
			
			title.setText("动态");
			if (l == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", "hlf");

				((LscActivity) activity).showProgress(activity);
				Task task = new Task(Task.TASK_MYDYNAMIC_LIST, obj);
				MainService.AddNewTask(task);
			}
		}

	};

	private OnClickListener aboutOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (Map.Entry<Button, View> entry : group.entrySet())
				entry.getValue().setVisibility(View.GONE);
			group.get(v).setVisibility(View.VISIBLE);
			dynamicListView = (ListView) group.get(v).findViewById(
					R.id.myFriendListView);

			title.setText(me.MemberName);
			if (gifAdapter == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", "hlf");

				((LscActivity) activity).showProgress(activity);
				Task task = new Task(Task.TASK_USER_HOME, obj);
				MainService.AddNewTask(task);
			}
		}

	};

	private GridView myGridView;
	private ArrayList<PhotoListParcelable> PhotoListParcelableList;
	private OnClickListener myPhotoOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (Map.Entry<Button, View> entry : group.entrySet())
				entry.getValue().setVisibility(View.GONE);
			if (group.get(v) == null) {// myphotodetail
				View view = inflater.inflate(R.layout.gridview, null);
				group.put((Button) v, view);
				ViewContainer.addView(view);
				myGridView = (GridView) view.findViewById(R.id.blog);
				myGridView.setOnItemClickListener(gridviewOnItemClickListener);

				// LoadImageFilter();
			}
			group.get(v).setVisibility(View.VISIBLE);

			title.setText("相册");
			if (p == null) {
				Map obj = new HashMap<String, String>();
				obj.put("AccountId", lscManager.getInstance().myAccount.name);

				((LscActivity) activity).showProgress(activity);
				Task task = new Task(Task.TASK_USER_PHOTOLIST, obj);
				MainService.AddNewTask(task);
			} else
				myGridView.setAdapter(photoAdapter);
		}

	};

	private OnItemClickListener gridviewOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			if (PhotoListParcelableList == null) {
				int len = p.size();
				PhotoListParcelableList = new ArrayList<PhotoListParcelable>();

				for (int n = 0; n < len; n++) {
					PhotoListParcelable pp = new PhotoListParcelable();
					pp.FileDesc = p.get(n).FileDesc;
					pp.FilePath = p.get(n).FilePath;
					pp.Id = p.get(n).Id;
					PhotoListParcelableList.add(pp);
				}
				Intent it = new Intent(activity, MyPhotoDetail.class);
				it.putParcelableArrayListExtra("list", PhotoListParcelableList);
				activity.startActivity(it);

			}
		}

	};

	@Override
	public void removeView() {
		// TODO Auto-generated method stub
		ViewContainer.removeAllViews();
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_MYDYNAMIC_LIST:
			l = (List<MyDynamicEntry>) param[1];
			if (l == null)
				return;
			myDynamicAdapter = new MyDynamicAdapter(activity, l);
			dynamicListView.setAdapter(myDynamicAdapter);
			break;
		case Task.TASK_USER_HOME: {
			m = (List<UserLoadGiftListEntry>) param[1];
			me = (MyDataEntry) param[2];
			if (m == null)
				return;
			gifAdapter = new GifAdapter(activity, m);
			dynamicListView.setAdapter(gifAdapter);

			this.Blood.setText(me.Blood);
			this.Age.setText(me.Age);
			this.MylinkCount.setText(me.MylinkCount);
			this.LinkmeCount.setText(me.LinkmeCount);
			this.Introduction.setText(me.Introduction);
			this.GiftCount.setText("收到" + me.GiftCount + "个礼物");
			this.title.setText(me.MemberName);

			Bitmap bm = null;
			NetImageBase n = new NetImageBase();
			if (me.PhotoUrl != null) {// 请求图标
				bm = n.getLogoBitmap(handler, activity,
						me.PhotoUrl,
						new imageCallback() {
							public void getImage(Bitmap bm) {
								// TODO Auto-generated method stub
								if (bm != null) {
									BitmapDrawable bd= new BitmapDrawable(activity.getResources(), bm); 
									myHomeBk.setBackgroundDrawable(bd);
									//getIsSelected().put(i, bm);
								}
							}
						});
				if(bm != null){
					bm = n.getLocalBitmap(null, activity, me.PhotoUrl, 450, 170);
					BitmapDrawable bd= new BitmapDrawable(activity.getResources(), bm); 
					myHomeBk.setBackgroundDrawable(bd);
				}
			}
		
			break;
		}
		case Task.TASK_USER_PHOTOLIST:
			p = (List<PhotoListEntry>) param[1];
			if (p == null)
				return;
			photoAdapter = new PhotoAdapter(activity, p);
			myGridView.setAdapter(photoAdapter);
			break;
		}
	}
	
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 5:{
				NetImageBase item = (NetImageBase) msg.obj;

				Bitmap bm = item.getLocalBitmap(null, activity, item.path, 80,
						80);
				item.ic.getImage(bm);
				break;
			}
			case 4:{
				NetImageBase item = (NetImageBase) msg.obj;

				Bitmap bm = item.getLocalBitmap(null, activity, item.path, 80,
						80);
				item.ic.getImage(BitmapFactory.decodeResource(activity.getResources(), R.drawable.dt_tx));
				break;
			}
			}
		}
	};
}
